/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolpresentation;

import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolservice.DTO.LectorDTO;
import com.pa165.languageschoolservice.DTO.LectureDTO;
import com.pa165.languageschoolservice.DTO.LevelofLanguageDTO;
import com.pa165.languageschoolservice.service.LectorService;
import com.pa165.languageschoolservice.service.LectureService;
import com.pa165.languageschoolservice.service.LevelOfLanguageService;
import java.text.SimpleDateFormat;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Elena Medvedeva
 */
@UrlBinding("/lectors/{$event}/{lectorDTO.id}/{levelOfLanguageDTO.id}/{lectureDTO.id}")
public class LectorActionBean implements ActionBean {

    final static Logger log = LoggerFactory.getLogger(LectorActionBean.class);
    private ActionBeanContext context;
    @SpringBean
    protected LectorService lectorService;
    @SpringBean
    protected LectureService lectureService;
    @SpringBean
    protected LevelOfLanguageService levelOfLanguageService;
    @ValidateNestedProperties(value = {
        @Validate(on = {"saveAdd", "saveEdit"}, field = "name", required = true),
        @Validate(on = {"saveAdd", "saveEdit"}, field = "surname", required = true)
    })
    private LectorDTO lectorDTO;
    private LevelofLanguageDTO levelOfLanguageDTO;
    private LectureDTO lectureDTO;
    private SimpleDateFormat sdfSource = new SimpleDateFormat(LectureActionBean.FORMATSTRING);

    public LectureDTO getLectureDTO() {
        return lectureDTO;
    }

    public void setLectureDTO(LectureDTO lectureDTO) {
        this.lectureDTO = lectureDTO;
    }

    public LevelofLanguageDTO getLevelOfLanguageDTO() {
        return levelOfLanguageDTO;
    }

    public void setLevelOfLanguageDTO(LevelofLanguageDTO levelOfLanguageDTO) {
        this.levelOfLanguageDTO = levelOfLanguageDTO;
    }

    public LectureService getLectureService() {
        return lectureService;
    }

    public void setLectureService(LectureService lectureService) {
        this.lectureService = lectureService;
    }

    public LectorService getLectorService() {
        return lectorService;
    }

    public void setLectorService(LectorService lectorService) {
        this.lectorService = lectorService;
    }

    public LevelOfLanguageService getLevelOfLanguageService() {
        return levelOfLanguageService;
    }

    public void setLevelOfLanguageService(LevelOfLanguageService levelOfLanguageService) {
        this.levelOfLanguageService = levelOfLanguageService;
    }

    public LectorDTO getLectorDTO() {
        return lectorDTO;
    }

    public void setLectorDTO(LectorDTO lectorDTO) {
        this.lectorDTO = lectorDTO;
    }

    public SimpleDateFormat getSdfSource() {
        return sdfSource;
    }

    public void setSdfSource(SimpleDateFormat sdfSource) {
        this.sdfSource = sdfSource;
    }

    public Resolution add() {
        log.debug("add() lectorDTO={}", lectorDTO);
        return new ForwardResolution("/lector/addLector.jsp");
    }

    public Resolution saveAdd() throws ServiceFailureException{
        log.debug("add() lectorDTO={}", lectorDTO);
        lectorService.add(lectorDTO);
        return new RedirectResolution(this.getClass(), "all");
    }

    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/lector/lector.jsp");
    }

    public Resolution delete() throws ServiceFailureException{
        log.debug("delete({})", lectorDTO.getId());
        lectorService.remove(lectorDTO);
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "saveEdit", "addLanguage", "showLectures", "enrollToLecture"})
    public void loadLectorFromDatabase() throws ServiceFailureException{
        String ids = context.getRequest().getParameter("lectorDTO.id");
        if (ids == null) {
            return;
        }
        lectorDTO = lectorService.get(Long.parseLong(ids));
    }

    public Resolution enrollToLecture() {
        log.debug(" enrollToLecture() Lecture={}", lectureDTO);
        try {
            lectorService.enrollLecture(lectureDTO, lectorDTO);
            return new RedirectResolution(this.getClass(), "all");
        } catch ( ServiceFailureException e) {
            ValidationErrors errors = new ValidationErrors();
            errors.add("lecture", new LocalizableError("lecture.empty"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }
    }

    public Resolution addLanguage() throws ServiceFailureException{
        levelOfLanguageDTO.setLector(lectorDTO);
        if (levelOfLanguageService.check(levelOfLanguageDTO)) {
            log.debug("addLanguage() Language={}", levelOfLanguageDTO);
            levelOfLanguageService.add(levelOfLanguageDTO);
            return getContext().getSourcePageResolution();
        } else {
            ValidationErrors errors = new ValidationErrors();
            errors.add("lector language", new LocalizableError("lectorLanguage.duplicate"));
            getContext().setValidationErrors(errors);
            return getContext().getSourcePageResolution();
        }
    }

    public Resolution deleteLanguage() throws ServiceFailureException{
        log.debug("deleteLanguage({})", levelOfLanguageDTO.getId());
        levelOfLanguageService.remove(levelOfLanguageDTO);
        return getContext().getSourcePageResolution();
    }

    public Resolution showLectures() {
        log.debug("showLectures()");
        return new ForwardResolution("/lector/lectorLectures.jsp");
    }

    public List<LectureDTO> getLectures() throws ServiceFailureException{
        log.debug("getLectures() lectorDTO={}", lectorDTO);
        return lectureService.getLectures(lectorDTO);
    }

    public List<LectureDTO> getAllLectures() {
        return lectureService.getAll();
    }

    public Resolution edit() {
        log.debug("edit() lector={}", lectorDTO);
        return new ForwardResolution("/lector/editLector.jsp");
    }

    public Resolution saveEdit() throws ServiceFailureException{
        log.debug("saveEdit() lectorDTO={}", lectorDTO);
        lectorService.modify(lectorDTO);
        return new RedirectResolution(this.getClass(), "all");
    }

    public List<LectorDTO> getLectors() {
        return lectorService.getAll();
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}
